{% extends 'layout/manage.html' %}

{% block css %}
    <style>
        .panel-default .panel-heading{
            display: flex;
            flex-direction: row;
            justify-content: space-between;
        }
        .title-list{
            border-right: 1px solid #dddddd;
            min-height: 500px;
        }
        .content{
            border-left: 1px solid #dddddd;
            min-height: 600px;
            margin-left: -1px;
        }

        .title-list ul{
            padding-left: 15px;
        }
        .title-list ul a {
            display: block;
            padding: 5px 0;
        }
    </style>

{% endblock %}

{% block content %}
    <div class="container-fluid">
        <div class="panel panel-default">
            <div class="panel-heading">
                <div>
                    <i class="fa fa-book" aria-hidden="true"></i>
                    wiki文档
                </div>

                <div class="function">
                    {% if wiki_obj %}
                        <a type="button" class="btn btn-success btn-xs"
                            href="{% url 'app01:wiki_delete' project_id=request.tracer.project.id wiki_id=wiki_obj.id %}">
                            <i class="fa fa-plus-circle" aria-hidden="true"></i> 删除
                        </a>
                        <a type="button" class="btn btn-success btn-xs"
                            href="{% url 'app01:wiki_edit' project_id=request.tracer.project.id wiki_id=wiki_obj.id %}">
                            <i class="fa fa-plus-circle" aria-hidden="true"></i> 编辑
                        </a>
                        <a type="button" class="btn btn-success btn-xs"
                            href="{% url 'app01:wiki_add' project_id=request.tracer.project.id %}">
                            <i class="fa fa-plus-circle" aria-hidden="true"></i> 新建
                        </a>
                    {% else %}
                        <a type="button" class="btn btn-success btn-xs"
                            href="{% url 'app01:wiki_add' project_id=request.tracer.project.id %}">
                            <i class="fa fa-plus-circle" aria-hidden="true"></i> 新建
                        </a>
                    {% endif %}

                </div>

            </div>
            <div class="col-sm-3 title-list">
                <ul id="catalog">
                    <!-- 等待后端传值生成对应的标签-->
                </ul>
            </div>
            <div class="col-sm-9 content">
                {% if wiki_obj %}
                    {{ wiki_obj.content }}
                {% else %}
                    <div style="text-align: center; margin-top:50px;">
                        <h4>《{{ request.tracer.project.name }}》wiki文档库</h4>
                        <a href="{% url 'app01:wiki_add' project_id=request.tracer.project.id %}">
                            <i class="fa fa-plus-circle" aria-hidden="true"></i> 新建文章
                        </a>
                    </div>
                {% endif %}
            </div>

        </div>
    </div>

{% endblock %}

{% block js %}
    <script>
        var WIKI_DETAIL_HREF = '{% url 'app01:wiki' project_id=request.tracer.project.id %}';
        $(function () {
            initCatalog();
        });

        function initCatalog() {
            $.ajax({
                url: '{% url "app01:wiki_catalog" project_id=request.tracer.project.id %}',
                type: 'GET',
                dataType: 'JSON',
                success: function (res) {
                    if (res.status){
                        $.each(res.data, function (index, item) {
                            var href = WIKI_DETAIL_HREF + '?wiki_id=' + item.id;
                            var li = $('<li>').attr('id', 'id_'+item.id).append($('<a>').text(item.title).attr('href', href)).append($('<ul>'));
                            if (!item.parent_id){
                                $('#catalog').append(li)
                            }else {
                                $('#id_'+item.parent_id).children('ul').append(li);
                            }
                        })
                    }
                }
            })
        }
    </script>
{% endblock %}